﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using SSN.Utilities.Security;
using SSN.Utilities.Cache;

namespace SSN.WebFramework
{
    public  class PageSecurityCache : KeyValueCache<string, List<PageSecurity>>
    {
        public  PageSecurityCache()
            : base(_AllSecurity, _PageSecurity, new TimeSpan(24, 0, 0))
        {
            
        }

        private static Dictionary<string, List<PageSecurity>> _AllSecurity()
        {
            Dictionary<string, List<PageSecurity>> securityTable = new Dictionary<string,List<PageSecurity>>();
            foreach (var p in PageSecurity.GetSecurityForApp(new AnonymousSecurityContext(), WebSettings.AppName))
            {
                if (!securityTable.ContainsKey(p.Path))
                {
                    securityTable[p.Path] = new List<PageSecurity>();
                }

                securityTable[p.Path].Add(p);
            }

            return securityTable;
        }

        private static List<PageSecurity> _PageSecurity(string page)
        {
            return PageSecurity.GetSecurityForPage(new AnonymousSecurityContext(), WebSettings.AppName, page);
        }
    }
}
